package com.drugStorage.struts.actions;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.drugStorage.domain.BuyList;
import com.drugStorage.domain.BuyPlan;
import com.drugStorage.domain.DrugScience;
import com.drugStorage.domain.DrugSupplier;
import com.drugStorage.domain.User;
import com.drugStorage.spring.service.BuyPlanService;
import com.drugStorage.spring.service.DrugService;
import com.drugStorage.spring.service.DrugSupplierService;
import com.drugStorage.struts.actionForms.BuyPlanActionForm;

public class BuyPlanAction extends DispatchAction {

	private BuyPlanService buyPlanService;

	private DrugService drugService;

	private DrugSupplierService drugSupplierService;

	public DrugService getDrugService() {
		return drugService;
	}

	public void setDrugService(DrugService drugService) {
		this.drugService = drugService;
	}

	public DrugSupplierService getDrugSupplierService() {
		return drugSupplierService;
	}

	public void setDrugSupplierService(DrugSupplierService drugSupplierService) {
		this.drugSupplierService = drugSupplierService;
	}

	public BuyPlanService getBuyPlanService() {
		return buyPlanService;
	}

	public void setBuyPlanService(BuyPlanService buyPlanService) {
		this.buyPlanService = buyPlanService;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");
		List<BuyPlan> buyPlanList = buyPlanService.getAllPlans();
		List<BuyPlan> buyPlanListUnchecked = buyPlanService
				.getPlanNotChecked(0);

		request.setAttribute("buyPlanList", buyPlanList);
		request.setAttribute("buyPlanUnchecked", buyPlanListUnchecked);
		// request.getSession().setAttribute("buyPlanList", buyPlanList);
		return mapping.findForward("BuyPlanList_success");
	}

	/**
	 * 通过计划单号获取
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getBuyLists(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");

		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");
		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		System.out.println(bid);
		List<BuyList> buyLists = buyPlanService.getAllListByPlanId(id);
		System.out.println(buyLists);
		request.setAttribute("buyLists", buyLists);
		// request.getSession().setAttribute("buyLists", buyLists);
		return mapping.findForward("BuyList_success");

	}

	/**
	 * 审核采购计划
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

	public ActionForward CheckBuyPlan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");

		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");
		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		BuyPlan buyPlan = buyPlanService.getBuyPlan(id);
		buyPlanService.CheckBuyPlan(buyPlan);
		List<BuyPlan> buyPlanList = buyPlanService.getAllPlans();
		request.setAttribute("buyPlanList", buyPlanList);
		List<BuyPlan> buyPlanListUnchecked = buyPlanService
				.getPlanNotChecked(0);

		request.setAttribute("buyPlanUnchecked", buyPlanListUnchecked);
		return mapping.findForward("BuyPlanList_success");
	}

	public ActionForward GoAddBuyPlan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		drugSupplierService = (DrugSupplierService) factory
				.getBean("drugSupplierService");
		List<DrugSupplier> drugSupplierList = drugSupplierService
				.getDrugSupplierList();
		System.out.println(drugSupplierList);
		request.setAttribute("drugSupplierList", drugSupplierList);

		
		return mapping.findForward("AddBuyPlan");
	}

	/**
	 * 添加采购计划单
	 */
	public ActionForward AddBuyPlan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");

		User user = (User) request.getSession().getAttribute("user");
		System.out.println(user);
		BuyPlanActionForm buyPlanActionForm = (BuyPlanActionForm) form;

		int sid = buyPlanActionForm.getDrugSupplierId();
		
		String dateStr = buyPlanActionForm.getDate();
		DrugSupplier drugSupplier = drugSupplierService.getDrugSupplier(sid);
		BuyPlan buyPlan = new BuyPlan();
		buyPlan.setOperator(user);

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		Date date = dateFormat.parse(dateStr);
		buyPlan.setDate(date);

		buyPlan.setDrugSupplier(drugSupplier);
		buyPlanService.saveBuyPlan(buyPlan);
		return mapping.findForward("AddBuyList_success");
	}

	/**
	 * 跳转到添加采购列表页面
	 */
	public ActionForward GoAddBuyList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		drugService = (DrugService) factory.getBean("drugService");
		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");
		// drugSupplierService = (DrugSupplierService)
		// factory.getBean("drugSupplierService");
		List<DrugScience> DrugList = drugService.showDrugs();
		// List<DrugSupplier> drugSupplierList =
		// drugSupplierService.getDrugSupplierList();

		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		BuyPlan buyPlan = buyPlanService.getBuyPlan(id);
		request.setAttribute("buyPlan", buyPlan);
		request.setAttribute("DrugList", DrugList);
		System.out.println("xxxx");
		// request.setAttribute("drugSupplierList", drugSupplierList);
		return mapping.findForward("AddBuyList");
	}

	/**
	 * 添加采购列表
	 */

	public ActionForward AddBuyList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");

		buyPlanService = (BuyPlanService) factory.getBean("buyPlanService");

		drugService = (DrugService) factory.getBean("drugService");
		List<DrugScience> DrugList = drugService.showDrugs();
		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		System.out.println(bid);
		BuyPlanActionForm buyPlanActionForm = (BuyPlanActionForm) form;
		int drugid = buyPlanActionForm.getDrugId();
		int amount = buyPlanActionForm.getAmount();
		float unitprice = buyPlanActionForm.getUnitPrice();
		DrugScience drug = drugService.getDrugById(drugid);
		System.out.println(drugid);
		float price = unitprice * amount;

		BuyList buyList = new BuyList();
		buyList.setAmount(amount);

		buyList.setDrug(drug);
		buyList.setPrice(price);
		buyList.setUnitPrice(unitprice);

		buyPlanService.AddBuyListToBuyPlan(id, buyList);
		List<BuyList> buyLists = buyPlanService.getAllListByPlanId(id);

		request.setAttribute("buyLists", buyLists);
		// 药品的列表

		request.setAttribute("DrugList", DrugList);

		// request.getSession().setAttribute("buyLists", buyLists);
		return mapping.findForward("AddBuyList_success");

	}

}
